#!/bin/bash
function display_usage {
cat <<HEREDOC
ftpsync-sh
Usage: ftpsync [MODE] [sitename]

Operation modes:
    -u          Update the local copy from the ftp site, when there is a conflict then the remote copy is downloaded.
                    Files are not deleted
    -a          Destructive Update, anything missing from the remote is deleted from the local copy
                    Files are deleted
    -c          Commit the local copy to the ftp site, when there is a conflict then the local copy is uploaded.
                    Files are not deleted
    -i          Destructive Commit, anything missing from local copy is deleted from remote copy
                    Files are deleted
    -s          When there are files that exist on only one side, then they are moved to the other side
                    Files are not deleted
    -p          Sync when local conflicts with remote, local wins and updates
                    Files are not deleted
    -r          Sync when remote conflicts with remote, remote wins and updates
                    Files are not deleted
                    
HEREDOC
exit 0
}

#Option Parsing
U='update';
C='commit';
I='destructive-commit';
A='destructive-update';
P='local-sync';
R='remote-sync';
S='sync';
SYNC=0; #SYNC is off
MODE="";
while getopts ":ucsai" opt ; do
    case $opt in
        u)
            MODE=$U
            ;;
        c)
            MODE=$C
            ;;
        s)
            MODE=$S;
            SYNC=1;#sync is on now
            ;;
        a)
            MODE=$A;
            ;;
        i)
            MODE=$I;
            ;;
        p)
            MODE=$P;
            ;;
        r)
            MODE=$R;
            ;;
        \?)
            echo "Invalid Option: -$OPTARG";
            display_usage;
            ;;
    esac
    shift $(($OPTIND - 1))
done
if [ $(echo -n $MODE | wc -c) -eq "0" ] ; then
    echo "No MODE specified"
    display_usage;
fi

SITE=$1;
if [ $(echo -n $SITE | wc -c) -eq "0" ] ; then
    display_usage;
    echo "No SITE specified";
    echo "Available Modes Are";
    echo $(ls ~/.ftpsync)
fi
